const { http } = require('../../../utils/util.js');

Page({
  data: {
    title: '',
    addr: '',
    phone: '',
    time: '',
    money_rmb: '',
    money_zwb: '',
    date: '',
    hour: '',
  },
  getLaunchInfo: function (e) {
    var type = e.target.dataset.type;
    this.setData({
      [type]: e.detail.value
    })
  },
  handleDateChange(e) {
    this.setData({
      date: e.detail.value,
    })
  },
  handleTimeChange(e) {
    this.setData({
      hour: e.detail.value,
    })
  },
  handleLaunch: function () {
    // console.log(this.data);
    const { title, addr, phone, time, money_rmb, money_zwb, date, hour } = this.data;
    if (!title) return this.toast('请输入项目名称');
    if (!addr) return this.toast('请输入门店地址');
    if (!phone) return this.toast('请输入联系方式');
    if (!/^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/.test(phone)) return this.toast('请输入正确的联系方式');
    if (!date) return this.toast('请选择维修日期');
    if (!hour) return this.toast('请选择维修时间');
    if (!money_rmb) return this.toast('请输入费用:人民币');
    if (!money_zwb) return this.toast('请输入费用:灶王币');
    this.setData({
      time: new Date(date + ' ' + hour).getTime() / 1000
    })
    http('/user/publish', 'post', this.data)
    .then((data) => {
      this.toast('发布成功');
      wx.navigateBack();
    })
    .catch(e => {
      this.toast(e.msg || '系统出错，请稍后再试');
    })
  },
  toast(title) {
    wx.showToast({
      title,
      icon: 'none'
    });
  },
  onLoad: function () {
  }
})
